<?php
/**
 * Created by PhpStorm.
 * User: fengbob
 * Date: 2018/1/22
 * Time: 12:02
 */

namespace app\models;

use app\librarys\DbCacheUtility;
use app\librarys\Tools;
use Codeception\Module\Db;

class GoodsBak
{

    /**
     * 添加商品
     */
    public static function add($mini_program_id,$name,$subtitle,$short_name,$category_id,$is_recommend,$is_newest,$is_hot,$is_free_express,$is_promotion
    ,$settlement_price,$selling_price,$old_price,$main_pic_url,$pic_urls,$sell_count,$collected_count,$express_moeny,$is_invoice,
                               $is_GGCT,$is_seven_day,$is_guarantee,$is_shelved,$is_return_goods,$stock)
    {
        $transaction = \Yii::$app->db->beginTransaction();
        try {
        $id = Tools::db_uuid();
       $cmd = \Yii::$app->db->createCommand('insert into t_goods(id,mini_programs_id`name`,subtitle,short_name,is_recommend,is_newest,is_hot,is_free_exxpress
is_promotion,settlement_price,selling_price,old_price,sell_count,collected_count,express_money,is_invoice,is_GGCT,is_seven_day,is_guarantee,
is_shelved,is_return_goods,stock)VALUES (:id,:mini_programs_id,:name,:subtitle,:short_name,:is_recommend,:is_newest,:is_hot,:is_free_exxpress,:is_promotion,:settlement_price,
:selling_price,:old_price,:sell_count,:collected_count,:express_money,:is_invoice,:is_GGCT,:is_seven_day,:is_guarantee,:is_shelved,:is_return_goods,:stock)');
       $cmd->bindValue(':id',$id);
            $cmd->bindValue(':mini_programs_id',$mini_program_id);
            $cmd->bindValue(':name',$name);
            $cmd->bindValue(':subtitle',$subtitle);
            $cmd->bindValue(':short_name',$short_name);
            $cmd->bindValue(':is_recommend',$is_recommend);
            $cmd->bindValue(':is_newest',$is_newest);
            $cmd->bindValue(':is_hot',$is_hot);
            $cmd->bindValue(':is_free_exxpress',$is_free_express);
            $cmd->bindValue(':is_promotion',$is_promotion);
            $cmd->bindValue(':settlement_price',$settlement_price);
            $cmd->bindValue(':selling_price',$selling_price);
            $cmd->bindValue(':old_price',$old_price);
            $cmd->bindValue(':sell_count',$sell_count);
            $cmd->bindValue(':collected_count',$collected_count);
            $cmd->bindValue(':express_money',$express_moeny);
            $cmd->bindValue(':is_invoice',$is_invoice);
            $cmd->bindValue(':is_GGCT',$is_GGCT);
            $cmd->bindValue(':is_seven_day',$is_seven_day);
            $cmd->bindValue(':is_guarantee',$is_guarantee);
            $cmd->bindValue(':is_shelved',$is_shelved);
            $cmd->bindValue(':is_return_goods',$is_return_goods);
            $cmd->bindValue(':stock',$stock);

            $re = DbCacheUtility::execute($cmd);

            //添加分类
            $pid = Tools::db_uuid();
            $sql = 'insert into t_goods_category_goods(id,goods_category_id,goods_id)values(:pid,:goods_category_id,:goods_id)';
            $cmd = \Yii::$app->db->createCommand($sql);
            $cmd->bindValue(':pid',$pid);
            $cmd->bindValue(':goods_category_id',$category_id);
            $cmd->bindValue(':goods_id',$id);
            $re = DbCacheUtility::execute($cmd);

            //添加主图片
            $pid = Tools::db_uuid();
            $sql = 'insert into t_goods_images(id,goods_id, `type`,image_url,image_sort)values(:pid,:good_id,1,:main_url,0)';
            $cmd = \Yii::$app->db->createCommand($sql);
            $cmd->bindValue(':pid',$pid);
            $cmd->bindValue(':good_id',$id);
            $cmd->bindValue(':main_url',$main_pic_url);
            $re = DbCacheUtility::execute($cmd);

            //添加图片
            if(is_array($pic_urls)){
                $so = 0;
                foreach ($pic_urls as $u){
                    $pid = Tools::db_uuid();
                    $sql = 'insert into t_goods_images(id,goods_id, `type`,image_url,image_sort)values(:pid,:good_id,2,:main_url,:sort)';
                    $cmd = \Yii::$app->db->createCommand($sql);
                    $cmd->bindValue(':pid',$pid);
                    $cmd->bindValue(':good_id',$id);
                    $cmd->bindValue(':main_url',$u);
                    $cmd->bindValue(':sort',$so);
                    $so = $so + 1;
                    $re = DbCacheUtility::execute($cmd);
                }
            }

             return $id;

        }catch (\Exception $e){
            $transaction->rollBack();
            throw $e;
        }
    }

    /**
     * 获取商品列表
     * @param $category_id 分类ID
     */
    public static function getGoodsList($category_id)
    {
        if($category_id != '') {
            //查出所有下级分类
            $cmd = \Yii::$app->db->createCommand('select id from t_goods_category where code like "(select code from t_goods_category where id=:category_id)%"');
            $cmd->bindValue(':category_id',$category_id);
            $results = DbCacheUtility::queryScalar();
        }
    }
}